<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Budget_model extends MY_Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	
	function Budget_model()
	{
		parent::MY_Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	function get_report_financial($uid, $permitid, $groupid, $startdate, $enddate, $typetime = 0, $page = 1, $numrow = 20 )
	{
		$out = new stdClass();
		
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_report_financial_admin($uid,$permitid,$groupid,$typetime,'$startdate','$enddate',$page,$numrow);") or die(mysqli_error());
		
		$out->items = array();
		while ($row = mysqli_fetch_object($result))
		{
			$out->items[] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$out->pages = mysqli_fetch_object($result);
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$out->summary = mysqli_fetch_object($result);

		mysqli_free_result($result);
		mysqli_close($iconn);
		
		return $out;
	}
	
	function get_report_financial_agency($groupid, $startdate, $enddate, $typetime = 0, $username = '', $page = 1, $numrow = 20 )
	{
		$out = new stdClass();
		
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_report_financial_agency($groupid,$typetime,'$startdate','$enddate',$page,$numrow,'$username');") or die(mysqli_error());
		
		$out->items = array();
		while ($row = mysqli_fetch_object($result))
		{
			$out->items[] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$out->pages = mysqli_fetch_object($result);
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$out->summary = mysqli_fetch_object($result);

		mysqli_free_result($result);
		mysqli_close($iconn);
		
		return $out;
	}
	
	function get_payment_logs($uid, $permitid, $fromdate, $todate, $type = -1, $status = -1, $page = 1, $limit = 20)
	{
		$out = new stdClass();
		
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_admin_get_payment_log($uid,$type,$status,$permitid,$page,$limit,'$fromdate','$todate');") or die(mysqli_error());
		
		$out->info = mysqli_fetch_object($result);
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$out->items = array();
		while ($row = mysqli_fetch_array($result))
		{
			$out->items[] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$out->totalPage = mysqli_fetch_object($result);
		$out->totalPage = $out->totalPage->totalpage;
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		
		return $out;
	}
	
	function get_group_info($gid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_group_info('$gid');") or die(mysqli_error());
		$row = mysqli_fetch_array($result);
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $row;
	}
	
	function get_user_info($uid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_user_info($uid);") or die(mysqli_error());
		$row = mysqli_fetch_object($result);
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $row;
	}
	
	function get_group_list()
	{
		$data = array();

		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_user_group_listall();") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
			$data[] = $row;
		}

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function get_payment_tranmiss($page = 1, $limit = 20)
	{
		$data = array();
		$data['items'] = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_payment_tranmiss_getall($page,$limit);") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_array($result);
		$data['pages'] = (int)$row['totalpage'];

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function get_payment_log_info($uid, $paykey)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_payment_info($uid, '$paykey');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        $row = mysqli_fetch_object($result);

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $row;
	}
	
	function get_paid_log_detail($userid, $dayView)
	{
		$data = array();

		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_payment_log_detail($userid, '$dayView');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
			$data[] = $row;
		}

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function payment_log_info_by_key($key)
	{
		return $this->getRows("CALL selfserving_get_payment_info_by_key('$key');", false, 'object');
	}
}